﻿Imports System.Net
Imports TNCBAL
Imports TNCDAL
Imports HTLibrary

Public Class ucLine
    Dim db As New TanNamChinhDataContext
    Private m_line As New CLine

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        DataGridViewHelper.DataGridView_InitEvent(dtgLine)
    End Sub

    Private Sub ucLine_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        KhoiTao_GiaoDien(True)
        KhoiTaodtgLine()
    End Sub

    Public Sub KhoiTaodtgLine(Optional ByVal NameUpdate As String = "", Optional ByVal Name As String = "")
        Dim fc = m_line.SelectAllRecordByName(Name)

        Dim source As New BindingSource
        source.DataSource = fc
        BindingNavigator1.BindingSource = source

        dtgLine.DataSource = source
        FormatdtgLine()

        Try
            If NameUpdate = "" Then
                dtgLine.Rows(0).Selected = True
                dtgLine.FirstDisplayedScrollingRowIndex = 0
            Else
                Dim index As Integer = 0
                For i As Integer = 0 To dtgLine.RowCount - 1
                    If dtgLine.Rows(i).Cells("LineName").Value = NameUpdate Then
                        index = i
                        Exit For
                    End If
                Next

                dtgLine.Rows(index).Cells("LineName").Selected = True
            End If
        Catch ex As Exception

        End Try
        
    End Sub

    Public Sub FormatdtgLine()
        'dtgLine.RowHeadersWidth = 20
        dtgLine.RowHeadersDefaultCellStyle.ForeColor = Color.Blue

        dtgLine.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill

        dtgLine.MultiSelect = False
        dtgLine.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        dtgLine.ReadOnly = True
        dtgLine.BackgroundColor = Color.White
        dtgLine.AllowUserToAddRows = False
        dtgLine.AllowUserToDeleteRows = False

        dtgLine.Columns("ID_Line").Visible = False
        dtgLine.Columns("IsActive").Visible = False
        With dtgLine.Columns("LineName")
            .HeaderText = "Line"
        End With
    End Sub

#Region "btn Them, Xoa, Sua"
    Dim f_Them As Boolean = False
    Dim f_Sua As Boolean = False

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Clear()
        txtLine.Focus()
        KhoiTao_GiaoDien(False)
        f_Them = True
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        KhoiTao_GiaoDien(False)
        txtLine.Focus()
        f_Sua = True
    End Sub

    Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
        Dim ID_Line As Integer = dtgLine.CurrentRow.Cells("ID_Line").Value
        'kiem tra truoc khi xoa
        Dim FCName As String = dtgLine.CurrentRow.Cells("LineName").Value
        If (MsgBox("Bạn muốn xóa " + FCName + " ra khỏi danh sách", MsgBoxStyle.YesNo, "Chú ý") = MsgBoxResult.Yes) Then
            Try
                m_line.Delete(ID_Line)
                KhoiTaodtgLine(0)
                'write systemlog
                Dim objLogController As New LogController
                objLogController.Insert(enumAction.Delete, String.Format("Line Name {0}", FCName))
            Catch ex As Exception
                MsgBox("Del - Line: " + FCName + " đang được sử dụng. Không xóa được")
            End Try
        End If
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Dim obj_line As New tblLine
        obj_line.LineName = txtLine.Text

        If f_Them Then
            obj_line.IsActive = True
            m_line.Insert(obj_line)
            KhoiTaodtgLine(txtLine.Text, "")
            'write systemlog
            Dim objLogController As New LogController
            objLogController.Insert(enumAction.Add, String.Format("Line Name {0}", obj_line.LineName))
        End If

        If f_Sua Then
            obj_line.ID_Line = dtgLine.CurrentRow.Cells("ID_Line").Value
            m_line.Update(obj_line)
            KhoiTaodtgLine(txtLine.Text, "")
            'write systemlog
            Dim objLogController As New LogController
            objLogController.Insert(enumAction.Edit, String.Format("Line Name {0}", obj_line.LineName))
        End If

        f_Them = False
        f_Sua = False
        KhoiTao_GiaoDien(True)
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        KhoiTao_GiaoDien(True)
        KhoiTaodtgLine()
        KhoiTaodtgLine(dtgLine.CurrentRow.Cells("LineName").Value, txtNameSearch.Text)
    End Sub

    Public Sub KhoiTao_GiaoDien(ByVal flag As Boolean)
        btnAdd.Enabled = flag
        btnEdit.Enabled = flag
        btnDel.Enabled = flag
        btnUpdate.Enabled = Not flag
        btnCancel.Enabled = Not flag
        txtLine.ReadOnly = flag

        dtgLine.Enabled = flag

        If flag Then
            txtLine.BackColor = Color.Azure
        Else
            txtLine.BackColor = Color.White
        End If
    End Sub

    Public Sub Clear()
        txtLine.Text = ""
    End Sub
#End Region

    Private Sub dtgLine_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtgLine.CurrentCellChanged
        Try
            txtLine.Text = dtgLine.CurrentRow.Cells("LineName").Value
        Catch ex As Exception

        End Try

    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        KhoiTaodtgLine("", txtNameSearch.Text.Trim)
    End Sub

    Private Sub btnCancelSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelSearch.Click
        txtNameSearch.Text = ""
        KhoiTaodtgLine("", txtNameSearch.Text.Trim)
    End Sub

    Private Sub txtNameSearch_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtNameSearch.KeyDown
        If e.KeyCode = Keys.Enter Then
            KhoiTaodtgLine("", txtNameSearch.Text.Trim)
        End If
    End Sub
End Class
